//
//  ZCCollectionTableViewCell.m
//  VoiceMusic
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 zhangdeyu. All rights reserved.
//

#import "ZCCollectionTableViewCell.h"
#import <Masonry.h>
#import "ZCMainCollectionViewCell.h"
#import "ZCMainModel.h"
#import <UIImageView+WebCache.h>
#import "ZCBaseLabel.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@implementation ZCCollectionTableViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self creatCollection];
    }
    return self;
}
-(void)creatCollection{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    flowLayout.itemSize = CGSizeMake(WIDTH / 3.5 , self.contentView.frame.size.height * 2.5 );
    flowLayout.minimumLineSpacing = 1;
    flowLayout.minimumInteritemSpacing = 1;
    
    flowLayout.sectionInset = UIEdgeInsetsMake(2, 5, 0, 2);
   
    //创建collectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    [self.contentView addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.pagingEnabled = YES;
    [self.collectionView registerClass:[ZCMainCollectionViewCell class]  forCellWithReuseIdentifier:@"reg"];
    
    //设置约束
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataCellArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *reg = @"reg";
    ZCMainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reg forIndexPath:indexPath];
    if(self.dataCellArr.count != 0){
        ZCMainModel *model = self.dataCellArr[indexPath.row];
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:model.picUrl] placeholderImage:[UIImage imageNamed:@"3187"]];
        cell.backgroundColor = [UIColor clearColor];
        cell.titleLabel.text = model.name;
        cell.titleLabel.numberOfLines = 0;
        cell.titleLabel.font = [UIFont systemFontOfSize:10];
    }
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ZCMainModel *main = self.dataCellArr[indexPath.row];
   
    //NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:main,@"1", nil];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"collection" object:nil userInfo:main.action];
}





- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
